/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brucexx.assistx.common.context;

import com.brucexx.assistx.util.StringUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author zhao.xiong
 */
public class ConfigContext {

    private static Properties p = new Properties();

    public static String getUrl(HttpServletRequest req) {
        if (p.containsKey("domainName")) {
            return "http://" + p.getProperty("domainName") + "/supergw_assistx";
        } else {
            return req.getContextPath();
        }
    }

    public static String getBasePath(HttpServletRequest req) {
        if (p.containsKey("domainName")) {
            return "http://" + p.getProperty("domainName");
        } else {
            return req.getContextPath();
        }
    }

    public static void init() throws IOException {
        File f = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "sgwConfig.properties");
        System.out.println(f.getPath());
        if (!f.exists()) {
            f.createNewFile();
            FileWriter fw = new FileWriter(f);
            try {
                fw.write("domainName=supergwinfo.alipay.net");
            } finally {
                IOUtils.closeQuietly(fw);
            }
        }
        FileInputStream fs = new FileInputStream(f);
        try {
            p.load(fs);
        } finally {
            IOUtils.closeQuietly(fs);
        }
    }

    public static void main(String args[]) throws IOException {
        init();
    }
}

